<?

	include("inc/config.inc.php");
    
	    include("inc/header.inc.php");

	$edit = mysqli_real_escape_string($con,$_GET[e]);

	// Check we are HR!
	if ($_SESSION[role] != "HR")
		die ("Error 41 - User error - Please contact support.");

	echo "<h2><span class='textColorChange'>$config[systemtitle]</span></h2><br/>\n\n";

	echo "<a href='main.php' style='color:black;font-size:10pt'><< Back</a><br/>";          

	echo "<div style='background:#504E52' class='normalText'>\n";
	echo "<b>Manage Users</b><br/><br/>\n";


	// Get all users, show prestages ones first as they require attention
	$result = mysqli_query($conn, "SELECT * FROM users WHERE deleted=0 ORDER BY prestaged DESC, lastname");
	if (mysqli_num_rows($result) > 0)
	{

		echo "<table BORDERCOLORLIGHT='#807E7C' BORDERCOLORDARK='#383634' border='1'><tr><td>User</td><td>Firstname</td><td>Lastname</td><td>Email</td><td>Startdate</td><td>Role</td><td>Manager</td><td>Ent.</td><td>Carried</td><td width='230'>Action</td></td></tr>";
		while ($user = mysqli_fetch_array($result))
		{

			if ($user[prestaged] == 1 && empty($edit))
				echo "<tr bgcolor='#B6010A'>\n";
			else
				echo "<tr>\n";

			// Row for editing
			if ($edit == $user[user])
			{
				// Form validation
				echo "<script>
				function validateForm()
				{
					if (document.forms['form']['firstname'].value == '' || document.forms['form']['lastname'].value == '' || document.forms['form']['email'].value == '' || document.forms['form']['entitlement'].value == '' || document.forms['form']['carriedover'].value == '')
					{
						alert('Please fill in all fields!');
						return false;
					}
				}


		                $(function() {
		                $('#datepicker').datepicker({
		                        dateFormat: 'yy-mm-dd'
		                });
		                });

				</script>
				";
				
				echo "<form name='form' action='updateuser.php' method='POST' onsubmit='return validateForm()'>";
				echo "<td><a name='edit'>$user[user]<input type='hidden' name='user' value='$user[user]'></td>\n";
				echo "<td><input type='text' name='firstname' value='$user[firstname]'></td>\n";
				echo "<td><input type='text' name='lastname' value='$user[lastname]'></td>\n";
				echo "<td><input type='text' name='email' value='$user[email]'></td>\n";
				echo "<td><input type='text' name='startdate' value='$user[startDate]' id='datepicker'></td>\n";
				echo "<td><select name='role'>\n";
				
				if ($user[role] == "Employee")
					echo "<option selected value='Employee'>Employee</option>\n";
				else
					echo "<option value='Employee'>Employee</option>\n";
				if ($user[role] == "Manager")
					echo "<option selected value='Manager'>Manager</option>\n";
				else
					echo "<option value='Manager'>Manager</option>\n";
				if ($user[role] == "HR")
					echo "<option selected value='HR'>HR</option>\n";
				else
					echo "<option value='HR'>HR</option>\n";
				
				
				echo "</select></td>\n";
				echo "<td><select name='manager'>\n";
				$mResult = mysqli_query($conn, "SELECT * FROM users WHERE (role='Manager' OR role='HR') AND deleted=0 ORDER BY user");
				while ($mgr = mysqli_fetch_array($mResult))
				{
					if ($mgr[user] != $user[manager]) 
						echo "<option value='$mgr[user]'>$mgr[user]</option>";
					else
						echo "<option selected value='$mgr[user]'>$mgr[user]</option>";
				}
				echo "</select></td>\n";
				echo "<td><input type='text' name='entitlement' value='$user[entitlement]'></td>\n";
				echo "<td><input type='text' name='carriedover' value='$user[carriedover]'></td>\n";
			}
			
			// Regular view row
			else
			{
				echo "<td><small>$user[user]</small></td>\n";
				echo "<td><small>$user[firstname]</small></td>\n";
				echo "<td><small>$user[lastname]</small></td>\n";
				echo "<td><small>$user[email]</small></td>\n";
				echo "<td><small>$user[startDate]</small></td>\n";
				echo "<td><small>" . ucFirst($user[role]) . "</small></td>\n";
				echo "<td><small>$user[manager]</small></td>\n";
				echo "<td><small>$user[entitlement]</small></td>\n";
				echo "<td><small>$user[carriedover]</small></td>\n";
			}

			// Actions
			if($edit == $user[user])
				echo "<td><input type='submit' value='Save'><input type='button' value='Cancel' onClick='location.href=\"users.php\"'></td></form></tr>\n";
			else
			{
				echo "<td><a href='users.php?e=$user[user]#edit'><img src='img/application_edit.png' width='16' height='16' border='0' alt='Edit'></a> \n";
				echo "<a href='userdel.php?e=$user[user]' onClick='return confirmDelete(\"delete\");'><img src='img/delete.png' width='16' height='16' border='0' alt='Delete'></a></td></tr>\n";
			}
				
		}
		echo "</table>\n";

	}

	echo "</div>\n";
	echo "\n<br/>\n\n";

	include("inc/footer.inc.php");
    
?>
    
